{$CLEO .s}
{$USE debug}
{$USE memory}
var 0@ : Integer
var 1@ : Integer
var 2@ : Integer
var 3@ : Integer
var 4@ : Integer
var 5@ : Integer
var 6@ : Integer
var 7@ : Integer
var 8@ : Integer
var 9@ : Integer
var 10@ : Integer

script_name "2401" // read_struct
debug_on


trace "2401 (read_struct)"

wait 0
// read 0 bytes
0AC6: get_label_pointer @DATA {store_to} 0@
1@ = 0xcccccccc
2@ = 0xdddddddd
3@ = 0xeeeeeeee

2401: read_struct 0@ {offset} 4 {size} 0 {store_to} 2@ // tested opcode
if and
    1@ == 0xcccccccc
    2@ == 0x00000000
    3@ == 0xeeeeeeee
then
    trace "~g~~h~~h~2401 (read_struct), #0 PASSED"
else
    breakpoint "~r~~h~~h~~h~2401 (read_struct), #0 FAILED!~n~cccccccc 00000000 eeeeeeee Expected~n~%08x %08x %08x Occured" 1@ 2@ 3@
end


wait 0
// read 1 byte
0AC6: get_label_pointer @DATA {store_to} 0@
1@ = 0xcccccccc
2@ = 0xdddddddd
3@ = 0xeeeeeeee

2401: read_struct 0@ {offset} 4 {size} 1 {store_to} 2@ // tested opcode
if and
    1@ == 0xcccccccc
    2@ == 0x00000044
    3@ == 0xeeeeeeee
then
    trace "~g~~h~~h~2401 (read_struct), #1 PASSED"
else
    breakpoint "~r~~h~~h~~h~2401 (read_struct), #1 FAILED!~n~cccccccc 00000044 eeeeeeee Expected~n~%08x %08x %08x Occured" 1@ 2@ 3@
end


wait 0
// read 2 bytes
0AC6: get_label_pointer @DATA {store_to} 0@
1@ = 0xcccccccc
2@ = 0xdddddddd
3@ = 0xeeeeeeee

2401: read_struct 0@ {offset} 4 {size} 2 {store_to} 2@ // tested opcode
if and
    1@ == 0xcccccccc
    2@ == 0x00003344
    3@ == 0xeeeeeeee
then
    trace "~g~~h~~h~2401 (read_struct), #2 PASSED"
else
    breakpoint "~r~~h~~h~~h~2401 (read_struct), #2 FAILED!~n~cccccccc 00003344 eeeeeeee Expected~n~%08x %08x %08x Occured" 1@ 2@ 3@
end


wait 0
// read 3 bytes
0AC6: get_label_pointer @DATA {store_to} 0@
1@ = 0xcccccccc
2@ = 0xdddddddd
3@ = 0xeeeeeeee

2401: read_struct 0@ {offset} 4 {size} 3 {store_to} 2@ // tested opcode
if and
    1@ == 0xcccccccc
    2@ == 0x00223344
    3@ == 0xeeeeeeee
then
    trace "~g~~h~~h~2401 (read_struct), #3 PASSED"
else
    breakpoint "~r~~h~~h~~h~2401 (read_struct), #3 FAILED!~n~cccccccc 00223344 eeeeeeee Expected~n~%08x %08x %08x Occured" 1@ 2@ 3@
end


wait 0
// read 4 bytes
0AC6: get_label_pointer @DATA {store_to} 0@
1@ = 0xcccccccc
2@ = 0xdddddddd
3@ = 0xeeeeeeee

2401: read_struct 0@ {offset} 4 {size} 4 {store_to} 2@ // tested opcode
if and
    1@ == 0xcccccccc
    2@ == 0x11223344
    3@ == 0xeeeeeeee
then
    trace "~g~~h~~h~2401 (read_struct), #4 PASSED"
else
    breakpoint "~r~~h~~h~~h~2401 (read_struct), #4 FAILED!~n~cccccccc 11223344 eeeeeeee Expected~n~%08x %08x %08x Occured" 1@ 2@ 3@
end


wait 0
// read 6 bytes - sizes greater than 4 are not supported by common variables (which are 4 bytes). Clamp to 4 bytes
0AC6: get_label_pointer @DATA {store_to} 0@
1@ = 0xcccccccc
2@ = 0xdddddddd
3@ = 0xeeeeeeee

2401: read_struct 0@ {offset} 4 {size} 6 {store_to} 2@ // tested opcode
if and
    1@ == 0xcccccccc
    2@ == 0x11223344
    3@ == 0xeeeeeeee
then
    trace "~g~~h~~h~2401 (read_struct), #5 PASSED (expected warning)"
else
    breakpoint "~r~~h~~h~~h~2401 (read_struct), #5 FAILED!~n~cccccccc 11223344 eeeeeeee Expected~n~%08x %08x %08x Occured" 1@ 2@ 3@
end


wait 0
// read string
0AC6: get_label_pointer @DATA {store_to} 0@
1@ = 0xcccccccc
2@s = 'CCCCCCCC'
4@ = 0xeeeeeeee

2401: read_struct 0@ {offset} 8 {size} 20 {store_to} 2@s // tested opcode
if and
    1@ == 0xcccccccc
    2@s == 'some lon' // trimmed by target value capacity
    4@ == 0xeeeeeeee
then
    trace "~g~~h~~h~2401 (read_struct), #6 PASSED"
else
    breakpoint "~r~~h~~h~~h~2401 (read_struct), #6 FAILED!~n~cccccccc 'some lon' eeeeeeee Expected~n~%08x %s %08x Occured" 1@ 2@s 4@
end


terminate_this_custom_script

:DATA
hex
    88 77 66 55 44 33 22 11
    "some longer testing text" 00
end
